﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using System.Text;

namespace He.Web.manage.attrfield
{
    public partial class edit : He.Web.UI.Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int channel_id;                                   //频道ID
        public int id;
        public Model.channel channel;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
                if (!new BLL.attr_field().Exists(this.id))
                {
                    ShowParamError();
                    return;
                }
                txtName.Enabled = false;
                ddlControlType.Enabled = false;
            }

            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                ShowParamError();
                return;
            }
            channel = new BLL.channel().GetModel(channel_id);
            if (!Page.IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editAttrfield" + channel_id);
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addAttrfield" + channel_id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.attr_field bll = new BLL.attr_field();
            Model.attr_field model = bll.GetModel(_id);
            txtTitle.Text = model.title;
            txtName.Text = model.name;
            ddlControlType.SelectedValue = model.control_type;
            rblEditorType.SelectedValue = model.editor_type.ToString();
            cbIsRequired.Checked = model.is_required == 1 ? true : false;
            cbIsLock.Checked = model.is_lock == 1 ? true : false;
            litAttrItems.Text = GetAttrItem(_id);
            txtDefaultValue.Text = model.default_value;
            txtTipMsg.Text = model.tip_msg;
            txtSortId.Text = model.sort_id.ToString();
            //显示对应的HTML
            ShowControlHtml(model.control_type); 
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.attr_field model = new He.Model.attr_field();
            BLL.attr_field bll = new BLL.attr_field();

            if (bll.Exists(txtName.Text.Trim(), this.channel_id))
            {
                ShowError("字段列名已存在，请重新输入");
                return false;
            }
            model.channel_id = channel_id;
            model.title = txtTitle.Text.Trim();
            model.name = txtName.Text.Trim();
            model.control_type = ddlControlType.SelectedValue;
            model.editor_type = int.Parse(rblEditorType.SelectedValue);
            model.is_required = cbIsRequired.Checked ? 1 : 0;
            model.is_lock = cbIsLock.Checked ? 1 : 0;
            model.default_value = txtDefaultValue.Text.Trim();
            model.tip_msg = txtTipMsg.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            int attrid = bll.Add(model);

            AddAttrItems(attrid);
            //保存日志
            SaveLogs(string.Format("[{0}管理]增加扩展字段:{1}", channel.manage_title, model.title));
            return true;

        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.attr_field bll = new BLL.attr_field();
            Model.attr_field model = bll.GetModel(this.id);

            model.title = txtTitle.Text.Trim();
            model.control_type = ddlControlType.SelectedValue;
            model.editor_type = int.Parse(rblEditorType.SelectedValue);
            model.is_required = cbIsRequired.Checked ? 1 : 0;
            model.is_lock = cbIsLock.Checked ? 1 : 0;
            model.default_value = txtDefaultValue.Text.Trim();
            model.tip_msg = txtTipMsg.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            bll.Update(model);
            //增加扩展选项
            AddAttrItems(this.id);
            SaveLogs(string.Format("[{0}管理]修改扩展属性:{1}", channel.manage_title, model.title));
            return true;
        }
        #endregion

        #region 赋值选项=================================
        private string GetAttrItem(int field_id)
        {
            List<Model.attr_item> itemList = new BLL.attr_item().GetModelList("field_id=" + field_id);
            StringBuilder strTemp = new StringBuilder();
            if (itemList != null)
            {
                foreach (Model.attr_item item in itemList)
                {
                    strTemp.AppendFormat("<span class=\"attritem\"><span class=\"span1\"><a>{0}</a></span><span class=\"span2\"><a href=\"javascript:delAttrItem('{1}');\">删除</a></span></span>", item.title, item.id);
                }
            }
            return strTemp.ToString();
        }
        #endregion

        #region 增加选项=================================
        public void AddAttrItems(int attrid)
        {
            //增加扩展选项
            if (ddlControlType.SelectedValue == "multi-radio" || ddlControlType.SelectedValue == "multi-checkbox")
            {
                string[] itemArr = txtAttrItems.Text.Trim().Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                if (itemArr.Length > 0)
                {
                    for (int i = 0; i < itemArr.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(itemArr[i]))
                        {
                            Model.attr_item attr_item = new Model.attr_item();
                            attr_item.field_id = attrid;
                            attr_item.control_type = ddlControlType.SelectedValue;
                            attr_item.title = itemArr[i];
                            attr_item.sort_id = 99;
                            new BLL.attr_item().Add(attr_item);
                        }
                    }
                }
            }
        }
        #endregion

        #region 显示对应的控件===========================
        private void ShowControlHtml(string control_type)
        {
            pEditorType.Visible = pAttrItems.Visible = false; //隐藏相应控件
            switch (control_type)
            {
                case "single-text": //单行文本
                    break;
                case "multi-text": //多行文本
                    break;
                case "editor": //编辑器
                    pEditorType.Visible = true;
                    break;
                case "number": //数字
                    break;
                case "checkbox": //复选框
                    break;
                case "multi-radio": //多项单选
                    pAttrItems.Visible = true;
                    break;
                case "multi-checkbox": //多项多选
                    pAttrItems.Visible = true;
                    break;
            }

        }
        #endregion

        //根据选择的控件类型显示相应部分
        protected void ddlControlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowControlHtml(ddlControlType.SelectedValue);
        }
        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改信息成功啦！", "list.aspx?channel_id=" + channel_id);
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("添加信息成功啦！", Request.RawUrl);
            }
        }


    }
}